﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI.WebControls;
using Rabbit.Logic;
using RabbitManage.Code;

namespace RabbitManage.UI
{
    public partial class MyOrders : System.Web.UI.Page
    {
        protected List<OrderViewShop> list = new List<OrderViewShop>();
        protected int AllCount;
        protected int WaitProcessCount;
        protected int SentCount;
        protected int FinishedCount;

        protected void Page_Load(object sender, EventArgs e)
        {
            HttpCookie cookie = Request.Cookies[Constant.CookieName];
            string shopId = cookie.Values[Constant.CookieUid];
            Shop shop = RabbitUse.GetShopByShopId(Db.Store, shopId);
            ShopOrderMapping shopOrderMapping = RabbitUse.GetShopOrderMappingById(Db.Store, shop.OrderMappingId);

            List<string> srcList = shopOrderMapping.OrderIdList;
            string category = Request["category"];
            if (category == OrderStateShop.WaitProcess.ToDescription()) srcList = shopOrderMapping.OrderIdListNew;
            if (category == OrderStateShop.Sent.ToDescription()) srcList = shopOrderMapping.OrderIdListSent;
            if (category == OrderStateShop.Finished.ToDescription()) srcList = shopOrderMapping.OrderIdListDone;
            AllCount = shopOrderMapping.OrderIdList.Count;
            WaitProcessCount = shopOrderMapping.OrderIdListNew.Count;
            SentCount = shopOrderMapping.OrderIdListSent.Count;
            FinishedCount = shopOrderMapping.OrderIdListDone.Count;

            int count = srcList.Count;
            for (int i = 0; i < count; i++)
            {
                string orderId = srcList[count - 1 - i];
                Order order = RabbitUse.GetOrderById(Db.Store, orderId);
                OrderViewShop orderViewShop = RabbitPunch.GetOrderViewShop(Db.Store, order);
                list.Add(orderViewShop);
            }

            myRepeater.ItemDataBound += Repeater_ItemDataBound;
            myRepeater.DataSource = list;
            myRepeater.DataBind();
        }

        void Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater repeater = e.Item.FindControl("productRepeater") as Repeater;
                OrderViewShop data = (OrderViewShop)e.Item.DataItem;
                repeater.DataSource = data.ProductSnapWithQuantityList;
                repeater.DataBind();
            }
        }
    }
}